{
 "cells": [
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# Solving Complex Tasks with Nested Chats\n",
    "\n",
    "This notebook shows how you can leverage **nested chats** to solve complex task with AG2. Nested chats is a sequence of chats created by a receiver agent after receiving a message from a sender agent and finished before the receiver agent replies to this message. Nested chats allow AG2 agents to use other agents as their inner monologue to accomplish tasks. This abstraction is powerful as it allows you to compose agents in rich ways. This notebook shows two basic examples of nested chat.\n",
    "\n",
    "\\:\\:\\:info Requirements\n",
    "\n",
    "Install `ag2`:\n",
    "```bash\n",
    "pip install ag2[openai]\n",
    "```\n",
    "\n",
    "For more information, please refer to the [installation guide](../../../../../docs/user-guide/basic-concepts/installing-ag2).\n",
    "\n",
    "\\:\\:\\:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import Annotated\n",
    "\n",
    "import autogen\n",
    "\n",
    "llm_config = autogen.LLMConfig.from_json(path=\"OAI_CONFIG_LIST\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\\:\\:\\:tip\n",
    "\n",
    "Learn more about the various ways to configure LLM endpoints [here](../../../../../docs/user-guide/basic-concepts/llm-configuration).\n",
    "\n",
    "\\:\\:\\:"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Example Task\n",
    "\n",
    "Suppose we want the agents to complete the following sequence of tasks:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "task = \"\"\"Write a concise but engaging blogpost about Navida.\"\"\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Scenario 1\n",
    "\n",
    "Let's say we desire the following workflow to solve the task: a user_proxy agent issues the initial query to a writer and acts as a proxy for the user. Whenever an initial writing is provided, a critic should be invoked to offer critique as feedback. This workflow can be realized by a three-agent system shown below. The system includes a user_proxy agent and a writer agent communicating with each other, with a critic agent nested within the user_proxy agent to provide critique. Whenever the user_proxy receives a message from the writer, it engages in a conversation with the critic agent to work out feedback on the writer's message.\n",
    "\n",
    "![](nested_chat_1.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Step 1. Define Agents\n",
    "Define the agents, including the outer agents writer and user_proxy, and the inner agent critic."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "writer = autogen.AssistantAgent(\n",
    "    name=\"Writer\",\n",
    "    llm_config=llm_config,\n",
    "    system_message=\"\"\"\n",
    "    You are a professional writer, known for your insightful and engaging articles.\n",
    "    You transform complex concepts into compelling narratives.\n",
    "    You should improve the quality of the content based on the feedback from the user.\n",
    "    \"\"\",\n",
    ")\n",
    "\n",
    "user_proxy = autogen.UserProxyAgent(\n",
    "    name=\"User\",\n",
    "    human_input_mode=\"NEVER\",\n",
    "    is_termination_msg=lambda x: x.get(\"content\", \"\").find(\"TERMINATE\") >= 0,\n",
    "    code_execution_config={\n",
    "        \"last_n_messages\": 1,\n",
    "        \"work_dir\": \"tasks\",\n",
    "        \"use_docker\": False,\n",
    "    },  # Please set use_docker=True if docker is available to run the generated code. Using docker is safer than running the generated code directly.\n",
    ")\n",
    "\n",
    "critic = autogen.AssistantAgent(\n",
    "    name=\"Critic\",\n",
    "    llm_config=llm_config,\n",
    "    system_message=\"\"\"\n",
    "    You are a critic, known for your thoroughness and commitment to standards.\n",
    "    Your task is to scrutinize content for any harmful elements or regulatory violations, ensuring\n",
    "    all materials align with required guidelines.\n",
    "    For code\n",
    "    \"\"\",\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Step 2: Orchestrate Nested Chats to Solve Tasks\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def reflection_message(recipient, messages, sender, config):\n",
    "    print(\"Reflecting...\", \"yellow\")\n",
    "    return f\"Reflect and provide critique on the following writing. \\n\\n {recipient.chat_messages_for_summary(sender)[-1]['content']}\"\n",
    "\n",
    "\n",
    "user_proxy.register_nested_chats(\n",
    "    [{\"recipient\": critic, \"message\": reflection_message, \"summary_method\": \"last_msg\", \"max_turns\": 1}],\n",
    "    trigger=writer,  # condition=my_condition,\n",
    ")\n",
    "\n",
    "res = user_proxy.initiate_chat(recipient=writer, message=task, max_turns=2, summary_method=\"last_msg\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Scenarios 2\n",
    "Let's say we desire the following workflow to solve the task. Compared to scenario 1, we want to include an additional `critic_executor` agent to chat with the `critic` and execute some tool calls involved in the chat. For example, a tool for detecting harmful content in the output of the writer.\n",
    "\n",
    "This workflow can be realized by a four-agent system shown below. The system includes a user_proxy agent and a writer agent communicating with each other, with a chat between the `critic` and `critic_executor` agent nested within the `user_proxy` agent to provide critique. Whenever the user_proxy receives a message from the writer, it engages in a conversation between `critic` and `critic_executor` to work out feedback on the writer's message. A summary of the nested conversation will be passed to the user_proxy, which will then be passed to the writer as feedback.\n",
    "\n",
    "![](nested_chat_2.png)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "critic_executor = autogen.UserProxyAgent(\n",
    "    name=\"Critic_Executor\",\n",
    "    human_input_mode=\"NEVER\",\n",
    "    # is_termination_msg=lambda x: x.get(\"content\", \"\").find(\"TERMINATE\") >= 0,\n",
    "    code_execution_config={\n",
    "        \"last_n_messages\": 1,\n",
    "        \"work_dir\": \"tasks\",\n",
    "        \"use_docker\": False,\n",
    "    },  # Please set use_docker=True if docker is available to run the generated code. Using docker is safer than running the generated code directly.\n",
    ")\n",
    "\n",
    "# one way of registering functions is to use the register_for_llm and register_for_execution decorators\n",
    "\n",
    "\n",
    "@critic_executor.register_for_execution()\n",
    "@critic.register_for_llm(name=\"check_harmful_content\", description=\"Check if content contain harmful keywords.\")\n",
    "def check_harmful_content(content: Annotated[str, \"Content to check if harmful keywords.\"]):\n",
    "    # List of harmful keywords for demonstration purposes\n",
    "    harmful_keywords = [\"violence\", \"hate\", \"bullying\", \"death\"]\n",
    "\n",
    "    # Normalize the input text to lower case to ensure case-insensitive matching\n",
    "    text = content.lower()\n",
    "\n",
    "    print(f\"Checking for harmful content...{text}\", \"yellow\")\n",
    "    # Check if any of the harmful keywords appear in the text\n",
    "    for keyword in harmful_keywords:\n",
    "        if keyword in text:\n",
    "            return \"Denied. Harmful content detected:\" + keyword  # Harmful content detected\n",
    "\n",
    "    return \"Approve. TERMINATE\"  # No harmful content detected\n",
    "\n",
    "\n",
    "def reflection_message_no_harm(recipient, messages, sender, config):\n",
    "    print(\"Reflecting...\", \"yellow\")\n",
    "    return f\"Reflect and provide critique on the following writing. Ensure it does not contain harmful content. You can use tools to check it. \\n\\n {recipient.chat_messages_for_summary(sender)[-1]['content']}\"\n",
    "\n",
    "\n",
    "user_proxy.register_nested_chats(\n",
    "    [\n",
    "        {\n",
    "            \"sender\": critic_executor,\n",
    "            \"recipient\": critic,\n",
    "            \"message\": reflection_message_no_harm,\n",
    "            \"max_turns\": 2,\n",
    "            \"summary_method\": \"last_msg\",\n",
    "        }\n",
    "    ],\n",
    "    trigger=writer,  # condition=my_condition,\n",
    ")\n",
    "\n",
    "res = user_proxy.initiate_chat(recipient=writer, message=task, max_turns=2, summary_method=\"last_msg\")"
   ]
  }
 ],
 "metadata": {
  "extra_files_to_copy": [
   "nested_chat_1.png",
   "nested_chat_2.png"
  ],
  "front_matter": {
   "description": "Solve complex tasks with a chat nested as inner monologue.",
   "tags": [
    "nested chat",
    "reflection",
    "reasoning",
    "orchestration"
   ]
  },
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.11"
  },
  "vscode": {
   "interpreter": {
    "hash": "949777d72b0d2535278d3dc13498b2535136f6dfe0678499012e853ee9abcab1"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
